<?php
session_start();
require('../FPDF/fpdf.php');
include("../includes/conexion.php");
class PDF extends FPDF
{
//Page header
function Header()
{
    $fechainicio=date('01/11/2008');
    $this->SetFont('Arial','B',14);
	//Move to the right
	$this->Cell(1);
	//Title
	$this->Cell(188,15,'UNIVERSIDAD NACIONAL  AGRARIA DE LA SELVA',0,0,'C');
	//Line break
	$this->Ln(14);

	$this->SetFont('Arial','B',13);
	//Move to the right
	$this->Cell(1);
	//Title
	$this->Cell(188,13,'Reporte totales de prestamos por especialidad',0,0,'C');
	//Line break
	$this->Ln(13);
	$this->SetFont('Arial','',9);
	//Move to the right
	$this->Cell(1);
	//Title
	$fechas=date("d/m/Y");
	$pages=split('/',$this->PageNo().'/{nb}');
	$this->Cell(15,10,'DEL',0,0,'L')+$this->Cell(20,10,$fechainicio,0,0,'R')+$this->Cell(20,10,'AL',0,0,'C')+$this->Cell(20,10,$fechas,0,0,'R')+$this->Cell(65,10,'PAGINA',0,0,'R')+$this->Cell(15,10,$pages[0],0,0,'C')+$this->Cell(15,10,'DE',0,0,'C')+$this->Cell(15,10,$pages[1],0,0,'C');
	//Line break
	$this->Ln(10);
	
    
}

//Page footer
function Footer()
{
	//Position at 1.5 cm from bottom
	$this->SetY(-15);
	//Arial italic 8
	$this->SetFont('Arial','I',8);
	//Page number
	$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}


function LoadData($fechaini)
{
global $handle;
$sql_user="
select e.des_especialidad,t.descripcion,count(*)as total from prestamos p 
              left join usuarios u on p.id_usuarios=u.id_usuarios
              left join especialidad e on e.id_especialidad = u.id_especialidad
              left join documento d on p.id_doc = d.id_doc
              left join tipo_documento t on d.id_tipo_documento = t.id_tipo_documento

              group by e.des_especialidad,t.descripcion
              order by e.des_especialidad,t.descripcion 

";

     $qry_user=mysql_query($sql_user);
	$data=array();
	while($lines=mysql_fetch_array($qry_user))
	   	$data[]=$lines;
	return $data;

 
}


function ImprovedTable($header,$data)
{   
  	$rowadd=0;
	//Column widths
	$w=array(100,36,30);
	$this->Cell(1);
	//Header
	for($i=0;$i<count($header);$i++)
		$this->Cell($w[$i]+8,7,$header[$i],1,0,'C');
	$this->Ln();
	//Data
	$this->Cell(1);
	foreach($data as $row)
	{
	    if ($rowadd==37){
		$this->Ln();
		$this->Cell(1);
		for($i=0;$i<count($header);$i++)
		$this->Cell($w[$i]+8,7,$header[$i],1,0,'C');
		$this->Ln();
		$this->Cell(1);
		$rowadd=0;
		
		}
        $this->Cell($w[0]+8,4,$row[0],'L',0,'L');
		$this->Cell($w[1]+8,4,$row[1],'LR',0,'L');
		$this->Cell($w[2]+8,4,$row[2],'R',0,'R');
		$this->Ln();
		$this->Cell(1);
		$rowadd = $rowadd +1;
		$sum = $sum + $row[2];
		
		}	   		
	    $this->Cell(190,0,'','T');
		$this->Ln();	
		$this->Cell(1);			
        $this->Cell($w[0]+8,6,'',1,0,'L');
		$this->Cell($w[1]+8,6,'TOTAL PRESTAMOS REALIZADOS',1,0,'L');
		$this->Cell($w[2]+8,6,$sum ,1,0,'R');
		$this->Ln();		
		

}

}


$pdf=new PDF();
$pdf->AliasNbPages();
//Column titles
$header=array('Especialidad','Tipo Documento','Total');
//Data loading
$data=$pdf->LoadData();
$pdf->SetFont('Arial','',7);
$pdf->AddPage();
$pdf->ImprovedTable($header,$data);
$pdf->Output("reportes_usuarios.pdf","F");

?>
